package dhy.lyb.change.controller;

import dhy.lyb.change.model.ExtendModel;
import dhy.lyb.change.model.Food;
import dhy.lyb.change.model.User;
import dhy.lyb.change.service.FoodService;
import dhy.lyb.change.service.UserService;
import dhy.lyb.change.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/11/29.
 */
@Controller
@Scope("prototype")
public class FoodInfoController extends BaseController {

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    @Qualifier("foodService")
    private FoodService foodService;

    /**
     * 食物信息接口
     *
     * @return
     */
    @RequestMapping(value = "food/getFoodList")
    @ResponseBody
    public List<Food> getFoodList() {
        List<Food> foods = foodService.getRecords();
        if (foods == null) {
            return null;
        }
        return foods;
    }

    @RequestMapping(value = "food/getUserFoods")
    @ResponseBody
    public List<Food> getUserFoods(@RequestBody User user) {
        if (user == null) {
            return null;
        }
        List<Food> foods = foodService.getFoodsByUser(user);
        if (foods == null) {
            return null;
        }
        return foods;
    }

    /**
     * 用户选择食物
     *
     * @param jsonCode
     * @return
     */
    @RequestMapping(value = "user/insertUserFoods")
    @ResponseBody
    public ExtendModel insertUserFoods(@RequestBody String jsonCode) {
        Map maps = JsonUtil.changeGsonToMaps(jsonCode);
        System.out.println(maps);
        double user_id = (Double) maps.get("user_id");
        String foodname = (String) maps.get("foodname");
        String content = (String) maps.get("content");
        String img = (String) maps.get("img");
        String material = (String) maps.get("material");

        Food food = new Food();
        food.setContent(content);
        food.setFoodname(foodname);
        food.setImg(img);
        food.setMaterial(material);

        foodService.insertRecord(food);

        if ("".equals(user_id) || user_id == 0 || "".equals(food.getId()) || food.getId() == 0) {
            return super.errorResult();
        }
        userService.insertUserFood((int) user_id, food.getId());
        return super.successResult(0);
    }

    /**
     * 用户删除食物
     *
     * @param jsonCode
     * @return
     */
    @RequestMapping(value = "user/deleteUserFoods")
    @ResponseBody
    public ExtendModel deleteUserFoods(@RequestBody String jsonCode) {
        Map<String, Double> maps = JsonUtil.changeGsonToMaps(jsonCode);
        System.out.println(maps);
        double user_id = maps.get("user_id");
        double food_id = maps.get("food_id");
        if ("".equals(user_id) || user_id == 0 || "".equals(food_id) || food_id == 0) {
            return super.errorResult();
        }
        userService.deleteUserFood((int) user_id, (int) food_id);
        return super.successResult(0);
    }
}
